﻿using OneP.InfinityScrollView;
using ProtoBuf.Message;
using System.Collections;
using System.Collections.Generic;
using UI;
using UnityEngine;
using UnityEngine.UI;

public class ChessRankItem : InfinityBaseItem
{
    public ChessRankPage chessRankPage;
    [SerializeField]
    private GroupSpriteKit topThree;
    [SerializeField]
    private Text rankNum;
    [SerializeField]
    private TextureKit heroIcon;
    [SerializeField]
    private Text heroName;
    [SerializeField]
    private GroupSpriteKit rankIconBg;
    [SerializeField]
    private GroupSpriteKit rankIcon;
    [SerializeField]
    private Text rankScore;
    [SerializeField]
    private GameObject bgMask;

    private string[] rankIconBgName = { "UI_zhuangbei_pinzhi_03", "UI_zhuangbei_pinzhi_02", "UI_zhuangbei_pinzhi_01" };


    public override void Reload(InfinityScrollView infinity, int _index)
    {
        base.Reload(infinity, _index);
        var currentRanks = chessRankPage.GetRankInfos();
        SetRankItemData(currentRanks[_index], _index + 1);
    }

    public void SetRankItemData(AutoChessRankPlayerInfo info, int rank)
    {
        if (info == null)
        {
            return;
        }
        topThree.SetGameObjectActiveEx(true);
        rankNum.SetGameObjectActiveEx(false);
        switch (rank)
        {
            case 1:
                topThree.SetSpriteName("UI_paihang_icon_1");
                break;
            case 2:
                topThree.SetSpriteName("UI_paihang_icon_2");
                break;
            case 3:
                topThree.SetSpriteName("UI_paihang_icon_3");
                break;
            default:
                rankNum.text = string.Format(TableTextBlo.GetText("ChessRunk"), rank.ToString());
                topThree.SetGameObjectActiveEx(false);
                rankNum.SetGameObjectActiveEx(true);
                break;
        }
        heroIcon.textureType = TextureKit.TextureType.HERO2D;
        heroIcon.TextureName = info.displayInformation.icon;
        heroName.text = info.displayInformation.nickName;
        if (info.grade >= 37)
        {
            info.grade = 37;
        }
        var autoChessGrade = Table.Blo.AutoChessBlo.GetGradeById(info.grade);
        if (autoChessGrade != null)
        {
            //for (int i = 0; i < rankIcons.Count; i++)
            //{
            //    rankIcons[i].SetActiveEx((i + 1) == autoChessGrade.rankType);
            //}

            int index = (autoChessGrade.rankType - 1) / 2;
            index = index > 2 ? 2 : index;
            rankIconBg.SetSpriteName(rankIconBgName[index]);
            rankIcon.SetSpriteName(autoChessGrade.gradeTextIcon);

            if (autoChessGrade.subGradeType == 0)
            {
                rankScore.text = (info.score - autoChessGrade.minScore).ToString();
            }
            else
            {
                rankScore.text = string.Format(TableTextBlo.GetText("AutoChess_Level_N"), autoChessGrade.subGradeType.ToString());
            }
        }
        if (rank % 2 == 1)
        {
            bgMask.SetActiveEx(false);
        }
    }

}
